<?php
class video_ppv extends Model
{
    public static function getTableName()
    {
        return 'video_ppv';
    }

    public function getPrimaryKey()
    {
        return array('video_id');
    }

    public function set($obj)
    {
        $this->video_id = $obj->id ? $obj->id : ''; 
        /*bug 5457
        if ('allow' == $obj->ppv_country_allow_deny) {
            $this->country_allow_list = is_null($obj->ppv_country) ? '' :  ParseCountryCode::json2country($obj->ppv_country);
            $this->country_deny_list = 'ALL';
        } else {
            $this->country_allow_list = 'ALL';
            $this->country_deny_list = is_null($obj->ppv_country) ? '' :  ParseCountryCode::json2country($obj->ppv_country);
        }
         */
        $this->country_allow_list = AC::DEFAULT_COUNTRY_ALLOW_LIST;
        $this->country_deny_list = AC::DEFAULT_COUNTRY_DENY_LIST;
        $this->publish_datetime = $obj->ppv_publish_datetime ? $obj->ppv_publish_datetime  : ''; 
        $this->expire_datetime  = $obj->ppv_expire_datetime ?  $obj->ppv_expire_datetime  : ''; 
    }

    public function get($id)
    {
        if($id)
        {
            $data = self::$dbo['r']->findInstance($this->getTableName(),$id);
        }else
        {
            $data =  self::$dbo['r']->find($this->getTableName());
        }
        if($data)
        {
            if('ALL' == $data->country_deny_list)
            {
                $data->country_allow_list = ParseCountryCode::country2json(AC::COUNTRY_LIST_CODE,$data->country_allow_list,0);
            }
            else
            {
                $data->country_deny_list = ParseCountryCode::country2json(AC::COUNTRY_LIST_CODE,$data->country_deny_list,1);
            }
        }
        else
        {
            $data = '';
        }
        return $data;
    }

	/*
	 * by donald
	 * at 2011-10-21
	 */
	public static function getOne($video_id)
	{
		return self::$dbo['w']->findInstance(__CLASS__, $video_id);
	}

    public function update($obj)
    {
        return Model::$dbo['w']->save($obj);
    }

    public function delete($id)
    {
        return Model::$dbo['w']->delete('video_ppv','video_id=?',array($id));
    }

	/*
	 * by donald
	 * modified at 2011-10-21
	 */
	public function add_change_log($programme_id, $is_active = 1)
	{
		$field['type'] = $this->getTableName();
		$field['keys'] = array('video_id' => $this->video_id);
		$field['is_active'] = $is_active;
		$field['programme_id'] = $programme_id;
		$field['episode_id'] = 0;
		$field['is_publish'] = 0;

		change_log::updateOne($field);
	}

	/*
	 * by donald
	 */
	public function delete_change_log()
	{
		$type = $this->getTableName();
		$keys = array('video_id' => $this->video_id);
		$is_publish = 0;

		change_log::delTypeAndKeysLog($type, $keys, $is_publish);
	}

}
?>
